package com.richest.api.utils;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.richest.api.service.IEatOrderNumberService;
import com.richest.bean.EatOrderNumberBean;
import com.richest.bean.FoodOrderBean;
import com.richest.bean.UserExtensionBean;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 点餐客户端常用工具
 *
 * @author zhaohongchao 2019/3/1 0001
 * @param
 * @return
 **/
public class ClientCNCommonUtils {
	/**
	 * 生成业务流水号
	 *
	 * @author zhaohongchao 2019/3/1 0001
	 * @param
	 * @return
	 **/
	public static String getSerialNumber(IEatOrderNumberService eatOrderNumberService, Long admart_id, Integer biz_type,
			String admart_code) {
		String strSerialNumber = "";
		String strNumber = "";
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		String strDate = format.format(new Date());
		Map<String, Object> params = new HashMap<>();
		params.put("admart_id", admart_id);
		params.put("date", strDate);
		EatOrderNumberBean eatOrderNumberBean = eatOrderNumberService.selectByIdAndDate(params);
		if (eatOrderNumberBean == null) {
			strNumber += "000000000001";
			eatOrderNumberBean = new EatOrderNumberBean();
			eatOrderNumberBean.setAdmart_id(admart_id);
			eatOrderNumberBean.setBiz_type(biz_type);
			eatOrderNumberBean.setDate(strDate);
			eatOrderNumberBean.setSerial_number(1L);

			eatOrderNumberService.insert(eatOrderNumberBean);
		} else {
			strNumber = String.format("%012d", eatOrderNumberBean.getSerial_number() + 1);
			eatOrderNumberBean.setSerial_number(eatOrderNumberBean.getSerial_number() + 1);

			eatOrderNumberService.update(eatOrderNumberBean);
		}

		// 8位日期
		strSerialNumber += strDate;
		// 6位业务类型
		strSerialNumber += String.format("%06d", Integer.valueOf(biz_type));
		// 6位商户码
		strSerialNumber += String.format("%06d", Integer.valueOf(admart_code));
		// 12位流水号
		strSerialNumber += strNumber;
		return strSerialNumber;
	}

	public static BigDecimal getEatOrderExtension(BigDecimal totalMoney, UserExtensionBean userExtensionBean) {
		BigDecimal bigDMoney = new BigDecimal("0");
		// 优惠卷+满减卷
		switch (userExtensionBean.getType()) {
		case 1:
		case 2: {
			bigDMoney = totalMoney.subtract(userExtensionBean.getMoney());
			break;
		}
		default:
			bigDMoney = totalMoney;
		}
		return bigDMoney;
	}

	public static void getFoodOrderExtension(UserExtensionBean userExtensionBean, FoodOrderBean foodOrderBean) {
		// 赠送金+折扣卡
		switch (userExtensionBean.getType()) {
		case 3:
		case 4: {
			JSONObject params = JSONObject.parseObject(userExtensionBean.getParams());
			String code = params.getString("code");
			BigDecimal value = new BigDecimal(params.getString("value"));
			if (StringUtils.equals("1", code)) { // 百分比
				BigDecimal bigDExtensionMoney = foodOrderBean.getPrice().multiply(value);
				if (userExtensionBean.getMoney().compareTo(new BigDecimal("0")) > 0) {
					if (userExtensionBean.getMoney().compareTo(bigDExtensionMoney) >= 0) {
						foodOrderBean.setPay_price(foodOrderBean.getPrice().subtract(bigDExtensionMoney));
						foodOrderBean.setPay_money(
								foodOrderBean.getPay_price().multiply(new BigDecimal(foodOrderBean.getCount())));

						userExtensionBean.setMoney(userExtensionBean.getMoney().subtract(bigDExtensionMoney));
					} else {
						foodOrderBean.setPay_price(foodOrderBean.getPrice().subtract(userExtensionBean.getMoney()));
						foodOrderBean.setPay_money(
								foodOrderBean.getPay_price().multiply(new BigDecimal(foodOrderBean.getCount())));

						userExtensionBean.setMoney(new BigDecimal("0"));
						userExtensionBean.setStatus(2);
					}
				}
			}
			break;
		}
		default:
		}
	}
}
